<template>
  <div>
    <el-dialog
      :title="obj.isadd ? '管理员添加' : '管理员编辑'"
      :visible.sync="obj.istrue"
      width="40%"
      @close="cancel()"
    >
      <span>
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="规格名称">
            <el-input v-model="form.specsname"></el-input>
          </el-form-item>
          <el-form-item  v-for="(item,index) in attrArr" :key="index" label="规格属性">
              <div class="arrs">
                  <el-input  placeholder="请输入规格" v-model="item.val"></el-input>
                  <el-button type="primary" v-if="index==0" @click="arradd">添加规格</el-button>
                  <el-button type="primary" v-else @click="arrpop">删除</el-button>
              </div>
          </el-form-item>
          <el-form-item label="状态">
            <el-switch
              v-model="form.status"
              :active-value="1"
              :inactive-value="2"
            ></el-switch>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit" v-if="obj.isadd"
          >确 定</el-button
        >
        <el-button type="primary" @click="edit" v-else>修改</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions } from "vuex";
import {  specsadd,specsinfo, specsedit } from "../../../requset/api";
import { successAlert, warningAlert } from "../../../util/alert.js";
export default {
  props: ["obj"],
  data() {
    return {
      attrArr:[{val:""},{val:""}],
      form: {
        specsname: "",
        attrs :"",
        status: 1,
      },
    };
  },
  computed: {
    // ...mapGetters({
    //   rolelist: "role/list",
    // }),
  },
  methods: {
    ...mapActions({
      reqChangeSpecs: "specs/reqChangeSpecs",
      reqChangeTotal: "specs/reqChangeTotal",
    }),
    emty() {
      this.form = {
        specsname: "",
        attrs :"",
        status: 1,
      };
      this.attrArr=[{val:""},{val:""}]
    },
    cancel() {
      this.obj.istrue = false;
      this.obj.isadd = true;
      this.emty();
    },
    arradd(){
      this.attrArr.push({val:""})
    },
    arrpop(index){
       this.attrArr.splice(index,1)
    },
    submit() {
      this.form.attrs=JSON.stringify(this.attrArr.map(item=> item.val))
      console.log(this.form.attrs);
      specsadd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.reqChangeSpecs();
          this.reqChangeTotal();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    getone(id) {
      specsinfo({ id: id }).then((res) => {
      this.attrArr=JSON.parse(res.data.list[0].attrs).map((item) => ({val: item}) );
      this.form=res.data.list[0];
      this.form.id=id
      })
    } ,
    edit(){
      specsedit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.reqChangeSpecs();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
  },
  mounted() {
    this.reqChangeSpecs();
  },
};
</script>

<style lang="scss" scoped>
.arrs{
  display: flex;
}
</style>